package com.gem.netty.service.gem;

import com.gem.entity.Game;
import com.gem.entity.User;
import com.gem.enums.ClientMessageType;
import com.gem.enums.GemType;
import com.gem.enums.MessageColorType;
import com.gem.factory.game.GameFactory;
import com.gem.netty.entity.ClientContent;
import com.gem.netty.entity.CurrentUser;
import com.gem.pipeline.BusinessProcess;
import com.gem.pipeline.ProcessContext;
import com.gem.utils.R;

import java.util.Map;
import java.util.TreeMap;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * Author: Xhy
 * CreateTime: 2022-09-25 13:50
 * 是否存入当前回合宝石库
 */
public class IsPutRoundGemService implements BusinessProcess<ClientContent> {
    @Override
    public void process(ProcessContext<ClientContent> context) {
        Game game = GameFactory.get(context.getData().getRoomId());
        CurrentUser user = game.getUsers().get(game.getCurrentIndex());
        Integer currentGem = user.getCurrentGetGemInfo();
        Map<Integer, Integer> gems = user.getCurrentGemState();
        boolean isNeedBreak = false;
        R r = new R();
        r.data("color", MessageColorType.SYSTEM_MESSAGE.type);
        r.type(ClientMessageType.GET_GEM.type);
        // 当前玩家已拥有10个宝石
        if (user.getGetGemNumber() == 10){
            isNeedBreak = true;
            context.setResult(r.message("你已经拥有10个宝石了"));
        }else if (gems.get(currentGem) == GemType.GOLD.type && gems.size() != 0){
            // 是否拿的是黄金,回合宝石库中没拿过宝石才可拿黄金
            isNeedBreak = true;
            context.setResult(r.message("你已拿过宝石,不能拿黄金了"));
        }else if (gems.containsKey(currentGem) && gems.size() == 2){
            // 当前只能拿3个不一样的，玩家却拿了2个一样的
            isNeedBreak = true;
            context.setResult(r.message("你只能拿3个不一样的宝石"));
        }else if (gems.containsKey(currentGem) && game.getGems().get(currentGem) <= game.getUsers().size()){
            // 拿两个一样的 宝石库小于等于场上人数则不能拿
            isNeedBreak = true;
            context.setResult(r.message("相应的宝石数量已经拿不了相同2个宝石了"));
        }else if (gems.containsKey(currentGem)){
            gems.put(currentGem,2);
        }else {
            // 拿的是黄金
            if (currentGem == GemType.GOLD.type && gems.size() >= 1){
                isNeedBreak = true;
                context.setResult(r.message("你拿过宝石了,不能拿黄金了"));
            }else {
                // 正常拿牌
                gems.put(currentGem,1);
            }

        }

        context.setNeedBreak(isNeedBreak);
    }
}
